package com.qf.game.dao;

import com.qf.framwork.base.BaseDao;
import com.qf.game.entity.Player;
import com.qf.game.entity.TongjiSex;
import com.qf.game.utils.Md5Utils;

import java.util.List;

/**
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
public class PlayerDao extends BaseDao {

    /**
     * 保存玩家
     * @param player
     * @return
     */
    public int insertPlayer(Player player){
        String sql = "insert into player(username, password, nickname, sex, email) value (?,?,?,?,?)";
        return super.insert(sql, player.getUsername(), Md5Utils.stringToMD5(player.getPassword()), player.getNickname(), player.getSex(), player.getEmail());
    }

    /**
     * 判断用户名是否存在
     * @param username
     * @return
     */
    public boolean existeUsername(String username) {
        String sql = "select count(*) from player where username = ?";
        int count = (int) super.query(sql, resultSet -> resultSet.next() ? resultSet.getInt(1) : 0, username);
        return count > 0 ? true : false;
    }

    /**
     * 根据用户名和密码查询用户信息
     * @param username
     * @param password
     * @return
     */
    public Player queryByUsername(String username, String password) {
        String sql = "select * from player where (username = ? or email = ?) and password = ?";
        return (Player) super.queryOne(sql, Player.class, username, username, Md5Utils.stringToMD5(password));
    }

    /**
     * 根据用户名查询用户信息
     * @param username
     * @return
     */
    public Player queryByUsername(String username) {
        String sql = "select * from player where username = ?";
        return (Player) super.queryOne(sql, Player.class, username);
    }

    /**
     * 修改密码
     * @param username
     * @param newPassword
     * @return
     */
    public int updatePassword(String username, String newPassword) {
        String sql = "update player set password = ? where username = ?";
        return super.update(sql, Md5Utils.stringToMD5(newPassword), username);
    }

    /**
     * 修改玩家信息
     * @param player
     * @return
     */
    public int updatePlayer(Player player){
        String sql = "update player set nickname = ?, sex = ?, email = ?, header = ?, happy_bean = ?, level = ?, update_time = ?, status = ? where id = ?";
        return super.update(sql, player.getNickname(), player.getSex(), player.getEmail(), player.getHeader(), player.getHappyBean(), player.getLevel(), player.getUpdateTime(), player.getStatus(), player.getId());
    }

    /**
     * 修改玩家的欢乐豆
     * @param count
     * @param type
     * @param pid
     * @return
     */
    public int updatePlayerHappyBean(int count, int type, int pid){
        String sql = "update player set happy_bean = happy_bean " + (type == 0 ? "+" : "-") + " ? where id = ?";
        return super.update(sql, count, pid);
    }

    /**
     * 获取性别的统计信息
     * @return
     */
    public List<TongjiSex> getSexDatas(){
        String sql = "select " +
                "   if(sex=0,'男','女') as 'sex'," +
                "   count(*) as 'count'," +
                "   (count(*)/(select count(*) from player)) as 'bili'" +
                " from player group by sex";
        return super.queryList(sql, TongjiSex.class);
    }
}
